"""
python 爬虫学习脚本：爬取微博热门话题榜
首先安装如下库的包
pip install requests  对网页的内容进行获取
pip install beautifulsoup4 解析HTML文档的库
pip install lxml lxml  解析库
pip install prettytable 实现表格打印

"""

import requests
from bs4 import BeautifulSoup
from prettytable import PrettyTable


class WeiboTop(object):
    def __init__(self):
        self.url = 'https://s.weibo.com/top/summary'  # 微博热搜地址

    def get_html(self):
        """获取网页内容"""
        r = requests.get(self.url)
        return r.text

    def analysis(self):
        """提取数据"""
        results = []
        htmls = self.get_html()
        soup = BeautifulSoup(htmls, 'lxml')
        ranks = soup.find_all('td', class_='td-01 ranktop')
        tags = soup.find_all('td', class_='td-02')
        hots = soup.find_all('td', class_='td-03')
        for rank, tag, hot in zip(ranks, tags, hots):
            results.append(
                [rank.string if rank else '',
                 tag.a.string,
                 tag.span.string if tag.span else 9999999,
                 hot.i.string if hot.i else ''])
        return results

    def __sort(self):
        """排序(倒序)"""
        result = self.analysis()
        return sorted(result, key=lambda x:[int(2)], reverse=False)  # 反向排序

    def show(self):
        """展示数据"""
        tb = PrettyTable()
        sorts_data = self.__sort()
        tb.field_names = '排名,热点话题,阅读数,热点类型'.split(',')
        for i in sorts_data:
            tb.add_row(i)
        print(tb)


if __name__ == "__main__":
    wt = WeiboTop()
    wt.show()